/*
 * TabHost.h
 *
 *  Created on: 2012-10-9
 *      Author: yangentao@gmail.com
 */

#ifndef TABHOST_H_
#define TABHOST_H_
#include <boost/shared_ptr.hpp>

#include "headers.h"
#include "Panel.h"
#include "WindowListenerAdapter.h"

using namespace std;

//TODO tooltip , context menu, close/closeOther/closeAll
//image, text, closeIcon, tabShape, backgroundBorder
class TabHost;
class TabPage{
public:
	TabHost* host;
	String text;
	HBITMAP image;
	boost::shared_ptr<Panel> page;
	Rgn region;
	int x;
	int y;
	int width;
	int height;
	int textWidth;
	int textHeight;
private:
public:
	TabPage();
	virtual ~TabPage();

	int calTabWidth(Canvas& g, int x, int y, int height);
	void drawTab(Canvas& g);
	Rect getCloseRect();
	Rect getRect();
	void drawClose(Canvas& g);
	void close();
private:
	Rect getIconRect();
	Rect getTextRect();

	void drawIcon(Canvas& g);
	void drawText(Canvas& g);


};

class TabListener{
public:
	virtual ~TabListener(){}
	virtual void onSelectedTabChanged(TabPage* oldPage, TabPage* newPage) = 0;
};

class TabHost : public Panel{
	typedef Panel Super;
public:
	Vector<boost::shared_ptr<TabPage> > pages;
	WindowListenerAdapter adapter;
	TabListener* listener;

private:
	HBRUSH brSelect;
	HBRUSH brFocus;
	HBITMAP bmSelect;
	HBITMAP bmFocus;
private:
//	Vector<int> stackSelect;
public:
	TabHost();
	virtual ~TabHost();
public:

	HWND appendTab(String label, HBITMAP image);
	HWND insertTab(int index, String label, HBITMAP image);

	Rect getTabBarRect();
	Rect getBodyRect();
	Rect getPageRect();

	HWND  currentPage();
	void removePage(HWND tab);

	TabPage* getTab(int i);
	int getSelectIndex();
	TabPage* getSelectTab();
	void closeTab(int index);

	bool hasFocus();
	void selectTab(int index);

public:
	void create(WndUtil parent, Rect r);
	void setTabListener(TabListener* listener);
	void onPaint(HWND hWnd, HDC dc, PAINTSTRUCT* ps);
	bool onClick(const  MouseEvent&  me);
	bool onMouseMove(const  MouseEvent&  me);
	bool onMouseHover(const  MouseEvent&  me);
	bool onMouseLeave(const  MouseEvent&  me);
	bool onMouseEnter(const  MouseEvent&  me);
	void onFocusChange(HWND hWnd, bool child, bool isKill);

private:
	void drawBar(Canvas& g);
	void drawBody(Canvas& g);
	void drawTabs(Canvas& g);
	void calRegion(Canvas& g, int index, Rgn& barRgn);

	HBRUSH getSelectBrush();
	HBRUSH getFocusBrush();

	void initBrushes();
	void cleanBrushes();

};

#endif /* TABHOST_H_ */
